class Solution {
public:
    int pre[200001]={0};
    void init(int n){
        for(int i=0;i<n;i++){
            pre[i]=i;
        }
    }

    int find(int a){
        if(pre[a] == a) return a;
        return pre[a] = find(pre[a]);
    }

    void joinin(int x,int y){
        if(find(x)!=find(y)){
            pre[find(x)]=find(y);
        }
    }

    bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
        if(source == destination) return true;
        init(n);
        for(auto i : edges){
            joinin(i[0],i[1]);
        }
        return find(source)==find(destination);
    }
};
